package com.example.exception.exception;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import java.io.IOException;
import java.util.Properties;

/**
 * 全局异常处理
 * 1.要求是配置类
 */
@Configuration
public class GlobalException2 {


    /**
     * 2. 返回对象类型必须是 SimpleMappingExceptionResolver
     * 缺点：只做异常映射视图，不能传递异常对象
     * @return SimpleMappingExceptionResolver
     * 可以将异常类型和该异常要跳转的视图做一个映射，即什么异常跳转什么视图
     */
    @Bean
    public SimpleMappingExceptionResolver getGlobalExceptionResolver() {
        //properties可以从properties 文件中加载
        Properties properties = new Properties();
        /**
         * properties 的 key 为异常的全限定路径
         *              value 为出现该异常，要跳转的视图
         */
        properties.put(IOException.class.getName(), "ioException");
        properties.put(ClassNotFoundException.class.getName(),"classException");

        properties.list(System.out);
        SimpleMappingExceptionResolver mapException = new SimpleMappingExceptionResolver();
        mapException.setExceptionMappings(properties);
        return mapException;
    }
}
